﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CWM.Client.Utils
{
    public class Converter
    {
        public static T Convert<T>(object value)
        {
            Type type = typeof(T);

            if (value == null || value == System.Convert.DBNull)
            {
                return default(T);
            }
            else
            {
                if (type.Equals(typeof(int?)) || type.Equals(typeof(short?)) || type.Equals(typeof(long?)))
                {
                    if (value.Equals("True"))
                    {
                        value = 1;
                    }
                    else if (value.Equals("False"))
                    {
                        value = 0;
                    }
                }
                if (type.Equals(typeof(bool?)) || type.Equals(typeof(bool)))
                {
                    if (value.Equals("0"))
                    {
                        value = false;
                    }
                    else
                    {
                        value = true;
                    }
                }

                if (IsNullableType(type))
                {
                    NullableConverter nullableConverter = new NullableConverter(type);
                    return (T)System.Convert.ChangeType(value, nullableConverter.UnderlyingType);
                }
                else
                {
                    return (T)System.Convert.ChangeType(value, type);
                }

            }
        }

        public static dynamic Convert(object value, Type type)
        {
            if (value == null || value == System.Convert.DBNull)
            {
                if (type.Equals(typeof(DateTime)))
                {
                    DateTime dt = new DateTime();
                    return dt;
                }
                else
                {
                    return null;
                }

            }

            else
            {
                if (type.Equals(typeof(int?)) || type.Equals(typeof(short?)) || type.Equals(typeof(long?)))
                {
                    if (value.Equals("True"))
                    {
                        value = 1;
                    }
                    else if (value.Equals("False"))
                    {
                        value = 0;
                    }
                }
                if (IsNullableType(type))
                {
                    NullableConverter nullableConverter = new NullableConverter(type);
                    return System.Convert.ChangeType(value, nullableConverter.UnderlyingType);
                }
                else
                {
                    return System.Convert.ChangeType(value, type);
                }
            }
        }

        private static bool IsNullableType(Type theType)
        {
            return (theType.IsGenericType && theType.
              GetGenericTypeDefinition().Equals
              (typeof(Nullable<>)));
        }
    }
}
